package com.adtech.adms.commons.web.filter;

import org.hibernate.SessionFactory;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

/**
 * 继承OpenSessionInViewFilter，扩展查找SessionFactory方法
 * 
 * @author Dormin
 * @since 2012-7-3
 * 
 * @Copyright (C) AD Tech Workshop All Right Reserved
 */
public class OpenSessionInViewFilter extends
		org.springframework.orm.hibernate3.support.OpenSessionInViewFilter {
	
	private static SessionFactory sessionFactory;
	
	public static void refreshSessionFactory(SessionFactory newSessionFactory) {
		sessionFactory = newSessionFactory;
	}
	
	/**
	 * 扩展查找SessionFactory方法
	 */
	protected SessionFactory lookupSessionFactory() {
		if (logger.isDebugEnabled()) {
			logger.debug("Using SessionFactory '" + getSessionFactoryBeanName() + "' for OpenSessionInViewFilter");
		}
		if(sessionFactory==null) {
			WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
			sessionFactory = wac.getBean(getSessionFactoryBeanName(), SessionFactory.class);
		}
		return sessionFactory;
	}
}
